home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: bcc.ac.uk!slidel
- From: slidel@bsm.bioc.ucl.ac.uk (Timothy Slidel)
- Subject: Re: do || die;
- Message-ID: <1996Mar7.212656.4757@ucl.ac.uk>
- Date: Thu, 7 Mar 1996 21:26:56 GMT
- References: <1996Mar7.052636.59812@ucl.ac.uk> <DnwJKs.KyJ@uns.bris.ac.uk>
- Organization: University College London
- X-Newsreader: TIN [version 1.2 PL2]
-
- Nathan Sidwell (nathan@pact.srf.ac.uk) wrote:
- : The expressions 'i && i--' looks like a simple boolean conditional.
- : However, your intent is the sideffect of i--. As such
- : if(i)
- : i--;
- : would be much clearer.
-
- Yes, but once you grasp the do || die concept it seems almost natural, as
- in:
-
- "Give me the money _or_ I'll blow your head off!!"
- "Give me the money _and_ you'll live"
-
- (too much Duke Nukem I think!!)
-
- What I'm trying to do is to extend two array markers outwards by one
- character without violating the array bounds, so I can do either:
-
- (s && s--) || (ary[e + 1] && e++);
- (ary[e + 1] && e++) || (s && s--);
-
- .. where the string array is terminated by '\0' ..
-
- or..
-
- if (s)
- s--;
- else if (ary[e + 1])
- e++;
- if (ary[e + 1])
- e++;
- else if (s)
- s--;
-
- Oops, hang on - yes I think the first bit is ok provided if s == 1 then
- (s && s--) still evaluates to true, (s && --s) would not... (I'm starting
- to lose my own argument here!). Without the Perl do || die concept in mind
- then the second case is clearer but just seems a bit long winded - would
- there be any performance increase in the first case - say if you had this
- in a loop or something?
-
- : Suppose I have a processing function which, if it fails I want to set an
- : error flag. What is the important thing here? setting the error flag
- : or processing the data? probably processing the data. Which of the following
- : two code fragments more clearly conveys what is happening?
- :
- : if(processing_function())
- : error = 1;
- : or
- : error |= processing_function();
-
- Ok - the second form here is not a good idea but:
-
- !processing_function() || (error = 1);
-
- perhaps is - emphasises the function call.
-
- error = processing_function();
-
- might be even better =:) What about:
-
- (s = fopen(blah...)) || error("Couldn't open ...");
-
- ?
-
- Are there any really bad things that can happen if you do this?
- Do any compilers produce an error? - is it portable?
-
- --
- Tim Slidel, Email: t.slidel@biochem.ucl.ac.uk
- BSM Unit, Biochemistry and Molecular Biology, Tel: +44 171419 3896
- University College, Gower Street, Fax: +44 171380 7193
- London WC1E 6BT, United Kingdom.
-
-